﻿using MQ.Core.Common;
using MQ.Core.Entity.System;
using MQ.Core.IRepository.System;
using MQ.Core.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Dapper;
using MQ.Core.Db;

namespace MQ.Core.Repository.System
{
    public class LogRepository : ILogRepository
    {
        private readonly IDbConnection db;
        private readonly DbOption dbOption;
        public LogRepository(DbOption dbOption)
        {
            this.db = DbFactory.SqlServer(dbOption.DefaultConnection);
        }
        public void Add(LogEntity entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(string ids)
        {
            db.Execute($"delete from sys_log where id in({ids})");
        }

        public void Delete(List<long> ids)
        {
            Delete(ids.Join(","));
        }

        public void Delete(long id)
        {
            db.Execute($"delete from sys_log where id={id}");
        }

        public IEnumerable<LogEntity> GetList(string query)
        {
            string sql = $"select * from sys_log where 1=1";
            var json = query.ToJObject();
            if (!json["type"].IsEmpty())
            {
                sql += $" and category={json["type"].ToString()}";
            }
            if (!json["level"].IsEmpty())
            {
                sql += $" and level='{json["level"].ToString()}'";
            }
            if (!json["keyword"].IsEmpty())
            {
                sql += $" and (msg like'%{json["keyword"].ToString()}%' or exception like'%{json["keyword"].ToString()}%')";
            }
            if (!json["startTime"].IsEmpty())
            {
                sql += $" and addtime>='{json["startTime"].ToString()}'";
            }
            if (!json["endTime"].IsEmpty())
            {
                sql += $" and addtime<='{json["endTime"].ToString()}'";
            }
            return db.Query<LogEntity>(sql);
        }

        public IEnumerable<LogEntity> GetPageList(Pagination pagination, string query)
        {
            string sql = $"select a.*,b.name AddUserName from sys_log a left join sys_user b on a.adduser=b.jobnum where 1=1";
            var json = query.ToJObject();
            if (!json["type"].IsEmpty())
            {
                sql += $" and category={json["type"].ToString()}";
            }
            if (!json["level"].IsEmpty())
            {
                sql += $" and level='{json["level"].ToString()}'";
            }
            if (!json["keyword"].IsEmpty())
            {
                sql += $" and (msg like'%{json["keyword"].ToString()}%' or exception like'%{json["keyword"].ToString()}%')";
            }
            if (!json["startTime"].IsEmpty())
            {
                sql += $" and a.addtime>='{json["startTime"].ToString()}'";
            }
            if (!json["endTime"].IsEmpty())
            {
                sql += $" and a.addtime<='{json["endTime"].ToString()}'";
            }
            return db.QueryPagination<LogEntity>(pagination,sql);
        }

        public void Update(LogEntity entity)
        {
            throw new NotImplementedException();
        }
    }
}
